package com.lanou.zy.projecta.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.NotesRecyclerViewAdapter;
import com.lanou.zy.projecta.entity.Notes;

import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class NotesFragment extends Fragment{
    private RecyclerView notesRecyclerView;
    private NotesRecyclerViewAdapter NotesRecyclerViewAdapter;
    private RequestQueue queue;
    private View view;

    public static NotesFragment newInstance(String id) {
        NotesFragment n = new NotesFragment();
        Bundle args = new Bundle();
        args.putString("id", id);
        n.setArguments(args);
        return n;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_notes, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
    }

    private void initView() {
        NotesRecyclerViewAdapter = new NotesRecyclerViewAdapter();
        notesRecyclerView = (RecyclerView) view.findViewById(R.id.notesRecyclerView);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(view.getContext(), 1);
        gridLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        notesRecyclerView.setLayoutManager(gridLayoutManager);
        notesRecyclerView.setAdapter(NotesRecyclerViewAdapter);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        String id = getArguments().getString("id");
        queue = Volley.newRequestQueue(getActivity());
        String url = "http://open.qyer.com/qyer/bbs/forum_thread_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=863811018458956&track_app_version=6.8.1&track_app_channel=taobao&track_device_info=msm8960&track_os=Android4.2.2&app_installtime=1445235475313&lat=38.88368&lon=121.544562&forum_id=" + id + "&forum_type=1,2,6&count=20&page=1&delcache=0";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                Notes n = gson.fromJson(response, Notes.class);
                Notes.DataEntity nd = n.getData();
                List<Notes.DataEntity.EntryEntity> nde = nd.getEntry();

                NotesRecyclerViewAdapter.addData(nde);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        queue.add(request);
    }

}
